//
//  YZMPageService.m
//  YZMicroStock
//
//  Created by wangminhong on 16/4/7.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMPageService.h"
#import "YZMSeverTimeService.h"

@implementation YZMPageService{
    
    NSInteger _maxPage;
    BOOL _changeFlag;
    NSInteger _changePage;
    NSString *_changeTimeEnd;
}

- (id)init{
    
    self = [super init];
    
    if (self) {
        
        [self reset];
    }
    
    return self;
}

- (void)reset{
    
    _page = -1;
    _pageSize = 10;
    _maxPage = -1;
    self.minPage = 1;
    _timeEnd = nil;
    _changeFlag = NO;
}

- (NSInteger)nextPage{
    
    if (_page <self.minPage) {
        
        return self.minPage;
    }
    
    return _page + 1;
}

- (BOOL)isMinPage{
    
    return _page==1?YES:NO;
}

- (BOOL)isMaxPage{
    
    if (_maxPage < 0 || self.tempPage < _maxPage) {
        
        return NO;
    }
    
    return YES;
}

- (id)copyWithZone:(NSZone *)zone {
    
    YZMPageService *copy = [[[self class] allocWithZone:zone] init];
    
    copy.page = self.page;
    copy.pageSize = self.pageSize;
    copy.maxPage = self.maxPage;
    copy.timeEnd = [self.timeEnd copyWithZone:zone];
    
    return copy;
}

- (void)preChangeWithPage:(NSInteger)page andTimeEnd:(BOOL)timeEndFlag{
    
    _changeFlag = YES;
    _changePage = page;
    if (timeEndFlag) {
        
        _changeTimeEnd = [NSString stringWithFormat:@"%@", @([YZMSeverTimeService sharedService].severTime *1000)] ;
        
    }else{
        
        _changeTimeEnd = nil;
    }
}

- (void)effectChange{
    
    if (_changeFlag) {
        
        _changeFlag = NO;
        _page = _changePage;
        if (_changeTimeEnd) {
            
            _timeEnd = _changeTimeEnd;
        }
    }
    
}
- (void)cancelChange{
    
    _changeFlag = NO;
}

- (NSInteger)tempPage{
    
    if (_changeFlag) {
        
        return _changePage;
    }
    
    return _page;
}
- (NSString *)tempTimeEnd{
    
    if (_changeFlag) {
        
        return _changeTimeEnd;
    }
    
    return _timeEnd;
}

@end
